#include <Arduino.h>
#include "global_config.h"

#include "ws2812b.h"
#include <WiFi.h>
#include <HTTPClient.h>

/*---------------------global variables----------------------------*/
WS2812B_RMT leds(NUM_LEDS, DATA_PIN);
String Date = "";

const char *wifiData[][2] = {
    {"昊翔电子", "88371720"}, // 替换为自己常用的wifi名和密码
    {"H3C_201", "cy0515cy0515"},
    // 继续添加需要的 Wi-Fi 名称和密码
};




/*---------------------private functions----------------------------*/
void wifiConnect(const char *wifiData[][2], int numNetworks);
void getTimeFromServer(void);


void setup() {

  Serial.begin(115200);

  #if 0
  Serial.printf("Deafult free size: %d\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
  Serial.printf("PSRAM free size: %d\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
  Serial.printf("Flash size: %d bytes\n", ESP.getFlashChipSize());
  #endif 


  pinMode(BSP_KEY, INPUT_PULLUP);
  //初始化板载 WS2812B 

  #if 0 
    // pinMode(ADC,ANALOG);
  
    pinMode(34, INPUT_PULLUP);
    pinMode(35, INPUT_PULLUP);
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
  #endif 

  //链接Wifi
  int numNetworks = sizeof(wifiData) / sizeof(wifiData[0]);
  wifiConnect(wifiData, numNetworks);

  getTimeFromServer();
  // put your setup code here, to run once:
}


void loop()
{

}



/*
*@func: 用于链接wifi的实际函数
*/
void wifiConnect(const char *wifiData[][2], int numNetworks)
{
    WiFi.disconnect(true);
    for (int i = 0; i < numNetworks; ++i)
    {
        const char *ssid = wifiData[i][0];
        const char *password = wifiData[i][1];

        Serial.print("Connecting to ");
        Serial.println(ssid);

        WiFi.begin(ssid, password);
        uint8_t count = 0;
        while (WiFi.status() != WL_CONNECTED)
        {
            //digitalWrite(led1, ledstatus);
            //ledstatus = !ledstatus;
            WS2812B_Toggle();


            Serial.print(".");
            count++;

            vTaskDelay(100);

            if (count >= 30) //如果尝试30次（等待3s）没成功
            {
                Serial.printf("\r\n-- wifi connect fail! --");
                break;
            }
        }

        if (WiFi.status() == WL_CONNECTED)
        {
            //如果连接成功，亮一下绿灯，然后熄灭
            leds.setBrightGreen();

            Serial.printf("\r\n-- wifi connect success! --\r\n");
            Serial.print("IP address: ");
            Serial.println(WiFi.localIP());
            Serial.println("Free Heap: " + String(ESP.getFreeHeap()));

            delayMicroseconds(1000);
            //vTaskDelay(1000);
            leds.turnOff(); //未知原因：无法让灯亮1s
            return; // 如果连接成功，退出函数
        }
    }
}


/*
*@func 从百度请求 GMT 时间（通过HTTP）
*/
void getTimeFromServer()
{
    String timeurl = "https://www.baidu.com";
    HTTPClient http;
    http.begin(timeurl);
    const char *headerKeys[] = {"Date"};
    http.collectHeaders(headerKeys, sizeof(headerKeys) / sizeof(headerKeys[0]));
    int httpCode = http.GET();
    Date = http.header("Date");//得到的是GMT时间
    Serial.println(Date);
    http.end();
    // delay(50); // 可以根据实际情况调整延时时间
}